unit uConsultarVendas;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.ExtCtrls,
  JvExStdCtrls, JvButton, JvCtrls, Vcl.Mask, JvExMask, JvToolEdit, Data.DB,
  Vcl.Grids, Vcl.DBGrids, Vcl.ComCtrls, JvDBGridFooter, JvExDBGrids, JvDBGrid,
  JvDBUltimGrid, Vcl.DBCtrls;

type
  TfrmConsultarVendas = class(TForm)
    GroupBox1: TGroupBox;
    GroupBox2: TGroupBox;
    GroupBox3: TGroupBox;
    TimerInicio: TTimer;
    btnFiltrar: TJvImgBtn;
    edtNomeCliente: TLabeledEdit;
    edtDataIni: TJvDateEdit;
    Label1: TLabel;
    edtDataFim: TJvDateEdit;
    Label2: TLabel;
    cbxTipoVenda: TComboBox;
    Label3: TLabel;
    edtNroCupom: TLabeledEdit;
    cbxFormaPagto: TComboBox;
    Label4: TLabel;
    DBGrid1: TDBGrid;
    dsTblnfs: TDataSource;
    DBGrid2: TDBGrid;
    dsTblins: TDataSource;
    Panel1: TPanel;
    DBEdit1: TDBEdit;
    Label5: TLabel;
    DBEdit2: TDBEdit;
    Label6: TLabel;
    Splitter1: TSplitter;
    Label7: TLabel;
    procedure TimerInicioTimer(Sender: TObject);
    procedure btnFiltrarClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormKeyPress(Sender: TObject; var Key: Char);
  private
    { Private declarations }
    procedure FiltrarNotas;

  public
    { Public declarations }
  end;

var
  frmConsultarVendas: TfrmConsultarVendas;

implementation

{$R *.dfm}

uses uDadosNotasSaida;

procedure TfrmConsultarVendas.btnFiltrarClick(Sender: TObject);
begin
  FiltrarNotas;
end;

procedure TfrmConsultarVendas.FiltrarNotas;
var
  SQL: String;
begin

  if (trim(edtDataIni.Text) <> '/  /') and (trim(edtDataFim.Text) <> '/  /') then
    SQL := SQL + ' and nfsdtemis between "' + FormatDateTime('yyyy-mm-dd', edtDataIni.Date) + '" and "' + FormatDateTime('yyyy-mm-dd', edtDataFim.Date) + '" ';

  if cbxTipoVenda.ItemIndex <> 2 then
    SQL := SQL + ' and nfstip = ' + IntToStr(cbxTipoVenda.ItemIndex);

  if cbxFormaPagto.ItemIndex <> 3 then
    SQL := SQL + ' and nfstippagto = ' + IntToStr(cbxFormaPagto.ItemIndex + 1);

  if Trim(edtNomeCliente.Text) <> EmptyStr then
    SQL := SQL + ' and nfsrazaosocial like "%' + trim(edtNomeCliente.Text) + '%" ';

  if Trim(edtNroCupom.Text) <> EmptyStr then
    SQL := SQL + ' and nfsnrocupom like "%' + trim(edtNroCupom.Text) + '%" ';

  SQL :=
    'select *      ' +
    '  from tblnfs ' +
    ' where 1 = 1  ' +
    SQL +
    ' order by nfsdtemis, nfscod, nfsserie';

  dmDadosNotasSaida.FiltrarNotasFiscaisSaida(SQL);

end;

procedure TfrmConsultarVendas.FormCreate(Sender: TObject);
begin
  edtDataIni.Date := StrToDate('01/' + FormatDateTime('mm/yyyy', Now));
  edtDataFim.Date := Now;
end;

procedure TfrmConsultarVendas.FormKeyPress(Sender: TObject; var Key: Char);
begin
  if key = #13 Then
  begin
    key := #0;
    Perform(Wm_NextDlgCtl,0,0);
  end;
end;

procedure TfrmConsultarVendas.TimerInicioTimer(Sender: TObject);
begin
  TimerInicio.Enabled := false;
  frmConsultarVendas.WindowState := wsMaximized;
end;

end.
